package org.shs.common.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.shs.user.model.UserModel;

/**
 * 
 * <br/>
 * <br/><b>Created:</b>&nbsp;&nbsp; Nov 5, 2008 4:34:05 PM
 *
 * @author anirvan
 *
 */
public class BaseDispatchAction extends DispatchAction
{
   /**
    * The logged in user's data set in the session.
    */
   protected UserModel userData = null;
   
   /* (non-Javadoc)
    * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
   public ActionForward execute(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response)
   throws Exception
   {
      HttpSession htSess = request.getSession(false);
      
      if(htSess != null)
      {
         userData = (UserModel)htSess.getAttribute("user");
         if(userData != null && userData.isValidated())
         {
            return super.execute(mapping, form, request, response);
         }
      }
      userData = new UserModel();
      userData.setMessage(SystemInitializer.app_msgs.getMessage("msg.sessInvalid"));
      request.setAttribute("user", userData);
      
      return mapping.findForward("logout");
   }
}
